import router from '../router'
import store from "../store"
import api from '../api'
import comFuc from "../utils/common"
import treeFuc from "../utils/tree"
import { isNull } from 'lodash-es'
const { varObj } = comFuc
const { childrenId } = treeFuc
const mainFuc = {
  upd (from:any, query:any, name:any, key:any, mark:any, formEl:any) {
    delete from.children
    from.o = query.length?query[0].o : query.o
    from.a = mark == '修改'? "修改" : '新增'
    from.t = mark == '修改'? "upd" : "add"
    if(key){
      from[key] = from[key]? from[key] : new Date().getTime()
    }
    let allFrom = [from]
    api.updateApi(allFrom).then((res:any) => {
      if(res.errcode == 0){
        ElNotification({ title: '提示', message: '操作成功', type: 'success' })
        store.dispatch('D', varObj(name, query))
        if(mark == '新增' && query.o == "o_sensor" && from.class=="0"){
          let form2 = {o: "o_sensor_star", a: '新增', t: 'add', sensor_id: from[key], offline: '1'}
          api.updateApi([form2]).then((res:any) => {
            console.log('生成star成功')
          })
        }
        return false
      }
    }).catch((err) => {
        ElNotification({ title: '提示', message: '操作失败', type: 'error' })
        return true
    })
  },
  del(from:any, query:any, name:any, tree:any, key:any) {
    if(from.hasOwnProperty('children')&&!isNull(from.children)) return ElNotification({ title: '提示', message: '请删除子节点', type: 'error' })
    ElMessageBox.confirm('是否确定删除?', '删除提示', {
      confirmButtonText: '确定',
      cancelButtonText: '关闭',
      type: 'error'
    }).then(() => {
      let o = query.length?query[0].o : query.o
      let allFrom:any = []
      if(tree){
        childrenId(tree, from[key], key).forEach((item:any) => {
          let json = { o: o, t: 'del', a: '删除'}
          json[key] = item
          allFrom.push(json)
        })
      }else{
        from.o = o
        from.t = "del"
        from.a = "删除"
        allFrom = [from]
      }
      api.updateApi(allFrom).then((res:any) => {
        if(res.errcode == 0){
          ElNotification({ title: '提示', message: '操作成功', type: 'success' })
          store.dispatch('D', varObj(name, query))
          if(query.o == "o_sensor" && from.class=="0"){
            let form2 = {o: "o_sensor_star", a: '删除', t: 'del', sensor_id: from.id}
            api.updateApi([form2]).then((res:any) => {
              console.log('删除star成功')
            })
          }
        }
      }).catch((err) => {
          ElNotification({ title: '提示', message: '操作失败', type: 'error' })
      })
    })
  },
  action(val:any, query:any, name:any) {
    if(!val.action) return ElNotification({ title: '提示', message: '请填写动作', type: 'error' })
    let params = {sensor_id: '', code: '', action: '' }
    params.sensor_id = val.sensor_id
    params.code = val.code
    params.action = val.action
    api.remote([params]).then((res:any) => {
      if(res.errcode == 0){
        ElNotification({ title: '提示', message: '操作成功', type: 'success' })
        store.dispatch('D', varObj(name, query))
      }
    }).catch((err) => {
        ElNotification({ title: '提示', message: '操作失败', type: 'error' })
    })
  },
  sync(dataForm:any, query:any, name:any, key:any) {
    // if(!dataForm[key]) return ElNotification({ title: '提示', message: '不能为空', type: 'error' })
    ElMessageBox.confirm('是否确定同步到同类型传感器?', '重要提示！', {
      confirmButtonText: '确定',
      cancelButtonText: '关闭',
      type: 'error'
    }).then(async() => {
      let tempArr:any = []
      let data = computed(()=>store.getters[name]).value.filter((a:any)=>a.type == dataForm.type)
      console.log('data', data)
      data.forEach((item:any) => {
        let temp = {"o": "o_sensor", "a": '同步', t: 'upd', id: item.id }
        temp[key] = dataForm[key]
        tempArr.push(temp)
      })
      console.log('------', tempArr)
      api.updateApi(tempArr).then((res:any) => {
      if(res.errcode == 0){
          ElNotification({ title: '提示', message: '同步成功', type: 'success' })
          store.dispatch('D', varObj(name, query))
          return false
        }
      }).catch((err) => {
          ElNotification({ title: '提示', message: '同步失败', type: 'error' })
          return true
      })
    })
  },
}

export default mainFuc
